package com.hanhanxiaochong.zhongxiangchuanmei.view.my.act;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.WorkAttendanceBean;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.WorkAttendancePresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.Lunar;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.RxTimerUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ToastUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IWorkAttendanceView;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.adapter.ClockListAdapter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 员工打卡
 */
public class WorkAttendanceActivity extends BaseActivity<WorkAttendancePresenter> implements IWorkAttendanceView {

    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.tv_current_date)
    TextView tvCurrentDate;
    @BindView(R.id.tv_current_lunar)
    TextView tvCurrentLunar;
    @BindView(R.id.tv_week)
    TextView tvWeek;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.tv_go_to_work)
    TextView tvGoToWork;
    @BindView(R.id.tv_go_off_work)
    TextView tvGoOffWork;

    private String token;
    private ClockListAdapter clockListAdapter;

    public static Intent createIntent(Context context) {
        return new Intent(context, WorkAttendanceActivity.class);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_work_attendace);
    }

    @Override
    protected void initPresenter() {
        mPresenter = new WorkAttendancePresenter(this);
        token = SharedPreferencesUtil.getsInstances(this).getLoginInfo().getToken();
    }

    @Override
    protected void init() {
        txtTitle.setText("打卡");
        long currentTimeMillis = System.currentTimeMillis();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(currentTimeMillis);
        CharSequence date = DateFormat.format("yyyy:MM:dd", calendar);
        //当前日期
        tvCurrentDate.setText(date.toString());
        //当前星期
        switch (calendar.get(Calendar.DAY_OF_WEEK)) {
            case 0:
                tvWeek.setText("周日");
                break;
            case 1:
                tvWeek.setText("周一");
                break;
            case 2:
                tvWeek.setText("周二");
                break;
            case 3:
                tvWeek.setText("周三");
                break;
            case 4:
                tvWeek.setText("周四");
                break;
            case 5:
                tvWeek.setText("周五");
                break;
            case 6:
                tvWeek.setText("周六");
                break;
        }

        //农历日期
        Lunar lunar = new Lunar(calendar);
        tvCurrentLunar.setText("农历" + lunar.toString());
        //当前时间
        RxTimerUtil.timer(1000, new RxTimerUtil.IRxNext() {
            @Override
            public void doNext(long number) {
                Long sysTime = System.currentTimeMillis();
                CharSequence sysTimeStr = DateFormat
                        .format("HH:mm", sysTime);
                tvTime.setText(sysTimeStr.toString());
            }

            @Override
            public void doComplete() {

            }
        });

        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        List<WorkAttendanceBean.LestBean> list = new ArrayList<>();
        clockListAdapter = new ClockListAdapter(this, R.layout.item_clock, list);
        recyclerview.setAdapter(clockListAdapter);
        mPresenter.getClockList(token);
    }

    @Override
    public void setClockList(WorkAttendanceBean data) {
        //types 1上班，2下班
        tvGoToWork.setBackground(data.getTypes() == 1? ResourcesUtils.getDrawable(R.drawable.shape_radius_blue):ResourcesUtils.getDrawable(R.drawable.shape_radius_gray));
        tvGoOffWork.setBackground(data.getTypes() != 1? ResourcesUtils.getDrawable(R.drawable.shape_radius_blue):ResourcesUtils.getDrawable(R.drawable.shape_radius_gray));
        tvGoToWork.setClickable(data.getTypes() == 1);
        tvGoOffWork.setClickable(data.getTypes() != 1);
        clockListAdapter.setDatas(data.getLest());
    }

    @Override
    public void clockSuccess(String data) {
        ToastUtils.showToast(data);
        mPresenter.getClockList(token);
    }

    @OnClick({R.id.ll_leftview, R.id.tv_go_to_work, R.id.tv_go_off_work})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
            case R.id.tv_go_to_work:
                mPresenter.clock(token, "1");
                break;
            case R.id.tv_go_off_work:
                mPresenter.clock(token, "2");
                break;
        }
    }

    @Override
    protected void onDestroy() {
        RxTimerUtil.cancel();
        super.onDestroy();
    }
}
